package com.interview.controller;

import com.interview.feign.ProviderApi;
import com.interview.ribbon.RibbonConsumerServiceAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * Created by fangjicai on 2020/4/9.
 */

@RestController
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private RibbonConsumerServiceAPI ribbonConsumerServiceAPI;

    @Resource
    private ProviderApi providerApi;

    @GetMapping("/findUserById/{id}")
    public String findUserById(@PathVariable("id")String id){
        //准备工作
        String hostName="localhost";
        int port=8401;
        String uri="/name/";
        //http://localhost:8401/name/100
        String url=new StringBuilder("http://").append(hostName).append(":")
                .append(port).append(uri).append(id).toString();
        String result = restTemplate.getForObject(url, String.class);
        return result;
    }

    @GetMapping("/ribbon/{id}")
    public String ribbionTest(@PathVariable("id") String id){
       return  this.ribbonConsumerServiceAPI.sayHello(id);
    }
    //localhost:8501/feigin/200
    @GetMapping("/feign/{id}")
    public String feignTest(@PathVariable("id") String id){
       return  this.providerApi.getName(id);
    }

}
